<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" cancel-button="none" :radius="100" placeholder="请输入关键词" focus="true" ></uni-search-bar>
    </view>
    
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="item in searchResults" :key="item.goods_id" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">{{ item.goods_name }}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="item in histories" :key="item"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 计时器
        timer: null,
        kw: '',
        // 搜索结果
        searchResults: [],
        // 搜索历史列表
        historyList: ['a', 'abc', 'h']
      };
    },
    methods: {
      // 输入事件 
      input(value) {
        // 清除原来的定时器
        clearTimeout(this.timer)
        // 设置新的定时器
        this.timer = setTimeout(() => {
          this.kw = value
          this.getSearchList()
        }, 500)
      },
      onLoad() {
        this.historyList = JSON.parse(uni.getStorageSync
        ('kw') || '[]')
      },
      // 获取搜索结果列表
      async getSearchList() {
        if(this.kw === ''){
          this.searchResults = []
          return
        }
        const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
      },
      saveSearchHistory() {
        // this.historyList.unshift(this.kw)
        const set = new Set(this.historyList)
        // console.log(set)
        set.delete(this.kw)
        set.add(this.kw)
        // 将新数组存入historyList
        this.historyList = Array.from(set)
        // 对搜索历史数据进行持久化处理
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      gotoDetail(goods_id) {
        // 点击搜索建议后将搜索框中的内容保存
        this.saveSearchHistory()
        // 然后跳转
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id 
        })
      }
    },
    computed: {
      histories() {
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0;
  
  z-index: 999;
}

// 搜索建议列表的样式
.sugg-list {
  padding: 0 5px;
  .sugg-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    margin: 10px 0;
    border: 1px solid #efefef;
    .goods-name {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

// 搜索历史
.history-box {
  padding: 0 5px;
  .history-title {
    display: flex;
    justify-content: space-between;
    height: 40px;
    align-items: center;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list {
    flex-wrap: wrap;
    .uni-tag {
      margin: 5px 5px 0 0;
    }
  }
}
</style>
